package cn.hk.common.utils;


import cn.hk.dept.domain.Area;
import cn.hk.dept.service.DeptService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DeptTreeUtil {

    @Autowired
    public DeptService deptService;

    @Autowired
    public RedisCache redisCache;

//    @Cacheable(cacheNames = "areaCash")
    public  List<Area> treeDeptBuild(List<Area> areaNodes, Long vaccinesId){

            ArrayList<Area> levelTress = CollUtil.newArrayList();
            for (Area areaNode: areaNodes) {
                if (areaNode.getLevel() == 1) {
                    levelTress.add(findChildren(areaNode,areaNodes,vaccinesId));
                }
            }
//            System.out.println("从mysql查询");
            return levelTress;
    }

    private  Area findChildren(Area curNode, List<Area> areaNodes, Long vaccinesId) {
        //初始化
        curNode.setAreaChildren(new ArrayList<Area>());
        //没有areaChildren，则为末节点
        areaNodes.stream().filter(per -> (
            per.getAreaCode().startsWith(curNode.getAreaCode())) && (per.getLevel() - 1) == curNode.getLevel()).forEach(per -> {
//            if (per.getLevel()==3) {
//                if (vaccinesId==null){
//                    //社区信息
////                    List<DeptVo> deptList = deptService.getDeptList(per);
////                    per.setDeptChildren(deptList);
//                }else {
//                    //社区信息+疫苗库存
////                     List<DeptVo> deptVaccinesList = deptService.getDeptVaccinesList(per,vaccinesId);
//////                    Console.log("deptVaccinesList",deptVaccinesList);
////                     per.setDeptChildren(deptVaccinesList);
//                }
//            }
            curNode.getAreaChildren().add(findChildren(per, areaNodes,vaccinesId));
        });
        return curNode;
    }
}



